package com.qing.expert.modules.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.expert.common.annotation.OperationLog;
import com.qing.expert.common.response.R;
import com.qing.expert.modules.user.service.UserService;
import com.qing.expert.modules.user.vo.request.BalanceRequest;
import com.qing.expert.modules.user.vo.request.UserUpdateRequest;
import com.qing.expert.modules.user.vo.response.BalanceRecordVO;
import com.qing.expert.modules.user.vo.response.FavoriteGoodsVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 更新用户信息
     */
    @OperationLog(action = "update_user_info", detail = "更新用户信息")
    @PutMapping("/info")
    public R<Void> updateUserInfo(
            @Validated @RequestBody UserUpdateRequest request,
            HttpServletRequest httpRequest) {
        String userId = (String) httpRequest.getAttribute("userId");
        userService.updateUserInfo(userId, request);
        return R.ok();
    }

    /**
     * 余额充值
     */
    @OperationLog(action = "recharge", detail = "用户充值")
    @PostMapping("/balance/recharge")
    public R<Void> recharge(
            @Validated @RequestBody BalanceRequest request,
            HttpServletRequest httpRequest) {
        // 校验充值金额是否为固定金额
        BigDecimal amount = request.getAmount();
        if (!isValidRechargeAmount(amount)) {
            return R.error("充值金额必须为100,200,300,500,1000");
        }

        String userId = (String) httpRequest.getAttribute("userId");
        userService.recharge(userId, request);
        return R.ok();
    }

    /**
     * 校验充值金额是否为固定金额
     */
    private boolean isValidRechargeAmount(BigDecimal amount) {
        BigDecimal[] validAmounts = {
                new BigDecimal("100"),
                new BigDecimal("200"),
                new BigDecimal("300"),
                new BigDecimal("500"),
                new BigDecimal("1000")
        };

        for (BigDecimal validAmount : validAmounts) {
            if (amount.compareTo(validAmount) == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 余额提现
     */
    @OperationLog(action = "withdraw", detail = "用户提现")
    @PostMapping("/balance/withdraw")
    public R<Void> withdraw(
            @Validated @RequestBody BalanceRequest request,
            HttpServletRequest httpRequest) {
        String userId = (String) httpRequest.getAttribute("userId");
        userService.withdraw(userId, request);
        return R.ok();
    }

    /**
     * 余额变动记录
     */
    @GetMapping("/balance/records")
    public R<Page<BalanceRecordVO>> getBalanceRecords(
            HttpServletRequest request,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        String userId = (String) request.getAttribute("userId");
        return R.ok(userService.getBalanceRecords(userId, page, size));
    }

    /**
     * 收藏列表
     */
    @GetMapping("/favorites")
    public R<Page<FavoriteGoodsVO>> getFavorites(
            HttpServletRequest request,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        String userId = (String) request.getAttribute("userId");
        return R.ok(userService.getFavorites(userId, page, size));
    }

    /**
     * 添加收藏
     */
    @OperationLog(action = "add_favorite", detail = "添加收藏")
    @PostMapping("/favorites/{goodsId}")
    public R<Void> addFavorite(
            @PathVariable String goodsId,
            HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        userService.addFavorite(userId, goodsId);
        return R.ok();
    }

    /**
     * 取消收藏
     */
    @OperationLog(action = "remove_favorite", detail = "取消收藏")
    @DeleteMapping("/favorites/{goodsId}")
    public R<Void> removeFavorite(
            @PathVariable String goodsId,
            HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        userService.removeFavorite(userId, goodsId);
        return R.ok();
    }
}